package com.atguigu.guli.service.edu.service.impl;

import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.CourseDescription;
import com.atguigu.guli.service.edu.entity.form.CourseInfoForm;
import com.atguigu.guli.service.edu.entity.vo.AdminCourseItemVo;
import com.atguigu.guli.service.edu.entity.vo.ApiCourseDetailVo;
import com.atguigu.guli.service.edu.entity.vo.CourseQueryVo;
import com.atguigu.guli.service.edu.mapper.CourseDescriptionMapper;
import com.atguigu.guli.service.edu.mapper.CourseMapper;
import com.atguigu.guli.service.edu.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author yz
 * @since 2022-07-18
 */
@Service
@Transactional
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Resource
    private CourseDescriptionMapper courseDescriptionMapper;

    @Override
    public String saveCourseInfo(CourseInfoForm courseInfoForm) {
        //将课程信息保存到 课程 和 课程简介表
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoForm, course);//将数据源相同的属性的值复制给目标对象course
        //baseMapper.insert(course);
        this.save(course);


        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseInfoForm.getDescription());
        courseDescription.setId(course.getId());
        courseDescriptionMapper.insert(courseDescription);
        return course.getId();
    }

    /**
     * 分页查询课程列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<AdminCourseItemVo> queryCourseItemVoPage(Integer pageNum, Integer pageSize) {
        Page<AdminCourseItemVo> page = new Page<>(pageNum, pageSize);
        List<AdminCourseItemVo> data = baseMapper.selectCourseCourseItemVoPage(page);
        page.setRecords(data);
        return page;
    }

    /**
     * 添加课程的回显
     *
     * @param courseId
     * @return
     */
    @Override
    public CourseInfoForm getCourseInfo(String courseId) {
        //通过课程id查询课程集合
        Course course = this.getById(courseId);
        CourseDescription courseDescription = courseDescriptionMapper.selectById(courseId);//通过课程id查询到课程详情集合
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        BeanUtils.copyProperties(course, courseInfoForm);//拷贝course中的数据到CourseInfoForm 里
        courseInfoForm.setDescription(courseDescription.getDescription());
        return courseInfoForm;
    }

    /**
     * 根据ID更新课程信息
     *
     * @param courseInfoForm
     * @param courseId
     */
    @Override
    public void updateCourseInfoFormById(CourseInfoForm courseInfoForm, String courseId) {
        //分步更新courseInfoForm信息
        //courseInfoForm内保存页面内课程发布前的基本信息
        //1.更新课程表内容
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoForm, course);//拷贝传入的courseInfoForm的属性到course中
        course.setId(courseId);
        baseMapper.updateById(course);

        //2.更新介绍表信息
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseInfoForm.getDescription());//设置课程信息
        courseDescription.setId(courseId);
        courseDescriptionMapper.updateById(courseDescription);
    }

    /**
     * 根据id删除课程
     *
     * @param courseId
     */
    @Override
    public void delteById(String courseId) {
        //根据课程id删除课程表与课程信息表对应的内容
        //1.自定义sql实现
        baseMapper.deleteCourseInfoById(courseId);
        //2.mp实现
        //LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        //CourseDescription courseDescription = courseDescriptionMapper.selectById(courseId);
        //Course course = baseMapper.selectById(courseId);
        //baseMapper.deleteById(courseId);
        //courseDescriptionMapper.deleteById(courseId);

    }

    @Override
    public AdminCourseItemVo getCoursePublishVo(String id) {
        /**
         * 自定义mapper
         */
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("t1.id", id);

        return baseMapper.selectCoursePublishVo(queryWrapper);
    }

    @Override
    public List<Course> queryCourseByConditon(CourseQueryVo courseQueryVo) {
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        String parentId = courseQueryVo.getSubjectParentId();
        String id = courseQueryVo.getSubjectId();
        Integer type = courseQueryVo.getType();
        Integer column = courseQueryVo.getOrderByColumn();

        //判断
        if (!StringUtils.isEmpty(parentId)) {//一级ID不为空
            queryWrapper.eq(Course::getSubjectParentId, parentId);
        }
        if (!StringUtils.isEmpty(id)) {//二级ID不为空
            queryWrapper.eq(Course::getSubjectId, id);
        }
        switch (column) {
            case 1://发布时间排序
                if (type == 1) {
                    queryWrapper.orderByAsc(Course::getPublishTime);//发布时间
                } else {
                    queryWrapper.orderByDesc(Course::getPublishTime);
                }
                break;
            case 2://价格排序
                if (type == 1) {
                    queryWrapper.orderByAsc(Course::getPrice);//发布价格
                } else {
                    queryWrapper.orderByDesc(Course::getPrice);
                }
                break;
            default://默认销量排序
                if (type == 1) {
                    queryWrapper.orderByAsc(Course::getPrice);//销量排序
                } else {
                    queryWrapper.orderByDesc(Course::getPrice);
                }

        }
        queryWrapper.eq(Course::getStatus,"Normal");//发布状态

        return this.list(queryWrapper);
    }

    /**
     * 根据ID查询课程详情
     * @param id
     * @return
     */
    @Override
    public ApiCourseDetailVo getCourseDetailVo(String id) {
        ApiCourseDetailVo detailVo = baseMapper.selectCourseDetailVoById(id);
        //更新课程浏览数量
        detailVo.setViewCount(detailVo.getViewCount()+1);//浏览量+1
        //更新课程表中数据
        Course course = new Course();
        course.setViewCount(detailVo.getViewCount());
        course.setId(id);
        baseMapper.updateById(course);
        return detailVo;
    }
}
